package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.geom.Vector3D;
	
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class FileManagers extends Sprite
	{
		private var fileList:Array = null;
		private var type:String = null;
		private var curDir:String = null;
		private var files:Array = null;
		
		public function FileManagers(files:Array = null, dir:String = null, type:String = null, fileRoot:String = null)
		{
			super();
			
			fileList = files;
			this.type = type;
			if (dir)
				this.curDir = dir.replace(/[\\\/]+$/g, "");
			trace("-----------new file Manager----------------", this.curDir, dirDepath);
			
			if (fileList == null)
			{
				SwfLoader.loadData("config.txt", initList);
			}
			else
			{
				if (stage) init();
				else addEventListener(Event.ADDED_TO_STAGE, init);
			}
		}
		
		public function get dirDepath():int
		{
			if (curDir)
				return curDir.split("/").length;
			return 0;
		}
		
		private function strInArr(str:String, arr:Array):Boolean
		{
			var isIn:Boolean = false;
			if (str)
			{
				for each (var s:String in arr)
				{
					if (s == str)
					{
						isIn = true;
						break;
					}
				}
			}
			return isIn;
		}
		
		private function pushStrToArr(str:String, arr:Array):Array
		{
			if (!strInArr(str, arr))
			{
				if (arr == null) arr = new Array();
				arr.push(str);
					//trace(str);
			}
			return arr;
		}
		
		private function fileInFiles(path:String):Boolean
		{
			if (files)
			{
				for each (var file:FileIcon in files)
				{
					if (file.dirDepth > dirDepath + 1)//dir
					{
						if (file.url == path)
							return true;
						if (file.getDirByLevel(dirDepath) == path.replace(/[\\\/]+$/g, ""))
							return true;
					}
					if (file.url == path)
						return true;
				}
			}
			//trace("---------not in-------");
			return false;
		}
		
		private function pushFileToFiles(file:FileIcon):Array
		{
			trace("push", file.url);
			if (file.url.lastIndexOf("/") == file.url.length - 1)
				return files;
			if (file.dirDepth > dirDepath + 1)//深度不同
			{
				var dir:String = file.getDirByLevel(dirDepath + 1);
				if (dir == null)
					return files;
				dir = dir.replace(/[\\\/]+$/, "") + "/";
				file = new FileIcon(dir);
			}
			else if (file.dirDepth <= dirDepath)//深度不够
			{
				return files;
			}
			if (fileInFiles(file.url))
				return files;
			if (files == null) files = new Array;
			files.push(file);
			trace("+++++++++++++++++++++");
			return files;
		}
		
		private function initList(e:Event = null):void
		{
			if (e && e.type == Event.COMPLETE)
			{
				var obj:Array = JSON.parse(e.target.data as String);
				for each (var o:Object in obj)
				{
					var s:String = o.name;
					if (s)
					{
						s = s.replace(/[\\\/]+/g, "/");
						fileList = pushStrToArr(s, fileList);
					}
				}
			}
			init();
		}
		
		private var mylist:MyList;
		
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addChild(new Bitmap(new BitmapData(stage.stageWidth, stage.stageHeight)));
			
			if (fileList == null)
			{
				trace("no file list");
				return;
			}
			trace(fileList.join("\n"));
			
			mylist = new MyList();
			mylist.addEventListener(Event.SELECT, on_click_show);
			mylist.MaskRect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
			addChild(mylist);
			trace("=========init=======");
			
			if (dirDepath > 0)
			{
				_file = new FileIcon(curDir + "/..", type);
				files = pushFileToFiles(_file);
			}
			for each (var url:String in fileList)
			{
				var _file:FileIcon = new FileIcon(url, type);
				files = pushFileToFiles(_file);
			}
			
			var i:int = 0;
			for each (var file:FileIcon in files)
			{
				if (file.filename)
				{
					file.title = file.filename;
				}
				else if (file.dir)
				{
					if (file.dir == curDir + "/")
						file.title = "..";
					else
						file.title = String(file.dir + "/");
				}
				else
				{
					trace("no title:", String(file.title).length, file.url, file.isDir);
				}
				
				var iconSize:int = 100 + 15;
				var cols:int = int(stage.stageWidth / iconSize);
				file.x = (i % cols) * iconSize;
				file.y = int(i / cols) * iconSize;
				file.name = "i" + i;
				mylist.addItem(file);
				++i;
			}
			trace("=========end=======");
		}
		private var subDirs:Array = new Array();
		
		private function on_click_show(e:Event):void
		{
			var target:FileIcon = mylist.ClickObject as FileIcon;
			var select_index:uint = uint(target.name.substr(1));
			//trace(select_index);
			//dispatchEvent(new Event(Event.SELECT));
			
			if (target.title == "..")
			{
				trace("..");
				parent.removeChild(this);
			}
			else if (target.filename && target.dirDepth - 1 == dirDepath)
			{
				trace("show file:", target.url);
				switch (type)
				{
				case "zip": 
					trace("===============zip");
					target.loadData(target.dataGetted);
					break;
				default: 
					target.loadData(target.dataGetted);
				}
			}
			else
			{
				trace("show dir:", target.url, target.dirDepth);
				if (subDirs[select_index] == null)
					subDirs[select_index] = new FileManagers(fileList, target.dir.replace(/[\\\/]+$/, ""), type);
				addChild(subDirs[select_index]);
			}
		}
	}
}
